<template>
    <div class="app-container">

        <!-- 计划管理列表 -->
        <div v-show="openDataScope">
            <el-divider content-position="left">计划管理列表</el-divider>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="Plus" @click="handleAdd"
                        v-hasPermi="['maintenance:plan:add']">新增</el-button>
                </el-col>
            </el-row>

            <!-- 表格数据 -->
            <el-table :data="maintenancePlanList">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column label="计划名称" prop="planName" min-width="220" />
                <el-table-column label="计划制定人" prop="userName" width="120" />
                <el-table-column label="计划制定时间" prop="createTime" width="200">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="计划开始时间" prop="startTime" width="200">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="计划结束时间" prop="endTime" width="200">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:menu:edit']">修改</el-button>
                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['maintenance:plan:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>


        <!-- 添加任务对话框 -->
        <el-dialog :title="title" :vsisible.sync="open" v-model="open" width="600px" append-to-body>
            <el-form :model="form" :rules="rules" ref="planRef" label-width="120px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="计划名称" prop="planName">
                            <el-input v-model="form.planName" placeholder="请输入计划名称" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="计划制定人" prop="userName">
                            <el-input disabled v-model="form.userName" placeholder="请输入计划制定人" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="计划制定时间" prop="createTime">
                            <el-date-picker v-model="form.createTime" style="width: 100%;" placeholder="请选择计划制定时间"
                                type="datetime"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="计划开始时间" prop="startTime">
                            <el-date-picker v-model="form.startTime" style="width: 100%;" placeholder="请选择计划开始时间"
                                type="datetime"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="计划结束时间" prop="endTime">
                            <el-date-picker v-model="form.endTime" style="width: 100%;" placeholder="请选择计划结束时间"
                                type="datetime"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <!-- 编辑 -->
        <div v-show="editScope">
            <el-divider content-position="left">编辑计划</el-divider>
            <el-descriptions>
                <el-descriptions-item label="计划名称：">{{ planInfo.planName }}</el-descriptions-item>
                <el-descriptions-item label="开始时间：">{{ parseTime(planInfo.createTime) }}</el-descriptions-item>
                <el-descriptions-item label="结束时间：">{{ parseTime(planInfo.endTime) }}</el-descriptions-item>
            </el-descriptions>
            <el-divider />
            <el-row class="mb-4">
                <el-button type="primary" plain @click="environmentButton">选择环境因子</el-button>
                <el-button type="primary" plain>重置环境因子</el-button>
                <el-button type="primary" plain @click="systemButton">新增列状态</el-button>
            </el-row>

            <el-divider />
            <el-row class="mb-4">
                <el-button type="primary" plain @click="addComponent">新增设备</el-button>
                <el-button type="primary" plain @click="deleteComponent">删除设备</el-button>
            </el-row>

            <el-table :data="componenList" @selection-change="componenSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="编号" prop="id" width="120" />
                <el-table-column label="设备名称" prop="componentName" min-width="220" />
                <el-table-column label="设备编码" prop="componentNumber" width="220" />
                <el-table-column label="停役时间" align="center" prop="stoptime" width="220">
                    <template #default="scopes">
                        <el-date-picker required @change="getXd(scopes.row)" v-model="scopes.row.stoptime"
                            style="width: 100%;" type="datetime"></el-date-picker>
                    </template>
                </el-table-column>
                <el-table-column label="复役时间" align="center" prop="resumetime" width="220">
                    <template #default="scopes">
                        <el-date-picker required v-model="scopes.row.resumetime" style="width: 100%;"
                            type="datetime"></el-date-picker>
                    </template>
                </el-table-column>
            </el-table>



            <!--选择环境因子-->
            <el-dialog title="环境因子选择" top="15vh" :vsisible.sync="environmentOpen" v-model="environmentOpen"
                width="600px" append-to-body draggable>
                <div class="app-container">
                    <el-form :model="form" :rules="rules" label-width="100px">
                        <el-row :gutter="20">
                            <el-col :span="20" :xs="24">
                                <el-form-item label="操作时间" prop="operatedate" style="width: 308px;">
                                    <el-date-picker required v-model="queryParams.operatedate" type="datetime"
                                        :rules="rules.timeRule"></el-date-picker>
                                </el-form-item>
                            </el-col>

                            <el-col :span="20" :xs="24">
                                <el-form-item label="" prop="misfirePolicy">
                                    <el-checkbox-group v-model="queryParams.environmentNumber">
                                        <el-checkbox :label="item.environmentNumber"
                                            v-for="(item, index) in environmentList" border>{{ item.environmentName
                                            }}</el-checkbox>
                                    </el-checkbox-group>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" :xs="24">
                                <el-button style="float: right;  margin-left: 10px" type="primary" plain icon="Check"
                                    @click="updateEnvironment">确认</el-button>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </el-dialog>


            <!--新增状态点-->
            <el-dialog title="新增状态点" top="15vh" :vsisible.sync="systemOpen" v-model="systemOpen" width="60%"
                append-to-body draggable>

                <div class="app-container">

                    <el-table :data="systemList" row-key="id" height="400px">
                        <el-table-column align="center" type="expand">
                            <template #default="scopes">
                                <el-table :data="scopes.row.systemList" hide-headers class="hide-table-header"
                                    row-key="id">
                                    <el-table-column label="列状态" align="center" prop="realname">
                                        <template #default="scopes">
                                            <el-select v-model="scopes.row.isRunning" @change="getXd(scopes.row)"
                                                class="m-2" placeholder="Select" size="small">
                                                <el-option
                                                    v-for="item in [{ label: '备用', value: '0' }, { label: '运行', value: '1' }]"
                                                    :key="item.value" :label="item.label" :value="item.value" />
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="运行备用列" align="center" prop="standbycolumnName" />
                                </el-table>
                            </template>
                        </el-table-column>
                        <el-table-column label="列状态" align="left" prop="groupName">
                            <template #default="scopes">
                                <span style="color: #409EFF">{{ scopes.row.groupName }}({{ scopes.row.cnt }}列)</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="运行备用列" align="center" prop="create_time" />
                    </el-table>
                    <el-row :gutter="20">
                        <el-col :span="12" :xs="24">
                            <el-button style="float: right;  margin-left: 10px" type="primary" plain icon="Check"
                                @click="updateSystem">确认</el-button>
                        </el-col>
                    </el-row>

                </div>

            </el-dialog>


            <el-dialog title="新增维修设备" top="15vh" :vsisible.sync="componentOpen" v-model="componentOpen" width="60%"
                append-to-body draggable>
                <Index1 @save="save"></Index1>
            </el-dialog>


        </div>

    </div>





</template>

<script setup name="Component">
import { listEnvironment } from "@/api/factoryInfo/environment/environment";
import { listMaintenancePlan, addMaintenancePlan, delMaintenancePlan } from "@/api/maintenance/plan/plan";
import { listSystem } from "@/api/factoryInfo/system1/system1";
import { getUserProfile } from "@/api/system/user";;
import Index1 from '@/views/maintenance/plan/index1.vue'
const { proxy } = getCurrentInstance();
import { ref } from 'vue';

/**维修设备 */
const componentOpen = ref(false);
const delComponenList = ref([]);
const componenList = ref([]);
const eventList = ref([]);

/**新增列状态 */
const systemList = ref([]);
const systemOpen = ref(false);


/**环境因子 */
const editScope = ref(false);
const environmentOpen = ref(false);
const environmentList = ref([]); //环境因子列表

/**计划管理列表 */
const openDataScope = ref(true);
const title = ref("");
const open = ref(false);
const maintenancePlanList = ref([]);
const total = ref(0);
const data = reactive({
    form: {
        userName: ""
    },
    queryParams: {
        pageNum: 1,
        pageSize: 5,
        environmentNumber: []
    },
    rules: {
        operatedate: [{ required: true, message: "请选择操作时间", trigger: "blur" }]
    },
});
const { queryParams, form, rules } = toRefs(data);

const planInfo = ref([]);

/** 重置操作表单 */
function reset() {
    form.value = {
        planName: undefined,
        userName: undefined,
        createTime: undefined,
        startTime: undefined,
        endTime: undefined
    };
    proxy.resetForm("userRef");
};


/** 查询列表 */
getList();
function getList() {
    debugger
    listMaintenancePlan(queryParams.value).then(response => {
        maintenancePlanList.value = response.rows;
        total.value = response.total;
    });
}


/** 取消按钮 */
function cancel() {
    reset();
    open.value = false;
};
/** 新增按钮操作 */
function handleAdd() {
    open.value = true;
    title.value = '新增计划'
    getUserProfile().then(response => {

        form.value.userName = response.data.userName;
    });
};



/** 提交按钮 */
function submitForm() {
    proxy.$refs["planRef"].validate(valid => {
        addMaintenancePlan(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            reset();
            getList();
        });
    });
};


/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id;
    proxy.$modal.confirm('是否确认删除该任务？').then(function () {
        return delMaintenancePlan(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
};

/** 修改按钮操作 */
function handleUpdate(row) {
    planInfo.value = row;
    openDataScope.value = false;
    editScope.value = true;

}

/**获取环境因子 */
function getEnvironmentList() {
    listEnvironment({}).then(response => {
        environmentList.value = response.data;
    });
}

//打开环境因子选择
function environmentButton() {
    environmentOpen.value = true;
    getEnvironmentList();
}

/**选择环境因子 */
function updateEnvironment() {
    environmentOpen.value = false;
}

/**列状态 */
function systemButton() {
    systemOpen.value = true;
    getSystemList();
}
function getSystemList() {
    listSystem(queryParams.value).then(response => {
        systemList.value = response.data;
    });
}

function updateSystem() {
    systemOpen.value = false;
}

/**设备维修 */
function addComponent() {
    componentOpen.value = true
}

function save(data1, data2) {
    eventList.value.push(data1);
    componenList.value.push(data2);
    componentOpen.value = false
}

/** 基本事件多选框选中数据 */
function componenSelectionChange(selection) {
    delComponenList.value = selection;
}


/**删除 */
function deleteComponent() {
    delComponenList.value.forEach(function (e, index, arr) {
        componenList.value.forEach(function (e1, index1, arr) {
            if (e.id == e1.id) {
                componenList.value.splice(index1, 1);
            }
        })
        eventList.value.forEach(function (e2, index2, arr) {
            if (e.id == e2.componentId) {
                eventList.value.splice(index2, 1);
            }
        })
    })
}

</script>


<style lang='scss' scoped>
/* 隐藏表头 */
::v-deep .hide-table-header .el-table__header-wrapper {
    display: none;
}
</style>